<?php
namespace app\api_wp_v01\controller;

class StaffIndex extends ApiWechatProgram {
	
	/**
	 * 申请状态
	 */
	public function get_apply_status() {
		
		$StaffApplyModel = \app\common\model\StaffApplyModel::where('subscriber_id', $this->subscriber_id)
			->order('id DESC')
			->limit(1)
			->find();
		
		$message = '';
		$have_apply = false;
		$is_success = false;
		if(null !== $StaffApplyModel){
			switch($StaffApplyModel->result){
				case 0 :	// 未审核
					$have_apply = true;
					break;
				case 1 :	// 已通过
					$have_apply = true;
					$is_success = true;
					$message = '您的申请已通过，即将跳转到首页';
					break;
				case 2 :	// 已拒绝
					$have_apply = false;
					$message = '您的上次申请被拒绝，请重新填写资料';
					break;
			};
		};
		
		return $this->disposeJson(0, $message, array(
			'have_apply' => $have_apply,
			'is_success' => $is_success,
		));
	}
	
	/**
	 * 申请提交
	 */
	public function make_apply() {
		
		// 校验传入参数
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('realname', $parameter) && is_string($parameter['realname']) && (1 === preg_match('/^[\x{4e00}-\x{9fa5}]{1,10}$/u', $parameter['realname']));
		$judge_2 = array_key_exists('phone', $parameter) && is_string($parameter['phone']) && (1 === preg_match('/^[0-9]{11}$/', $parameter['phone']));
		$judge_3 = array_key_exists('supervise_region_1', $parameter) && is_array($parameter['supervise_region_1']);
		$judge_4 = array_key_exists('supervise_region_2', $parameter) && (is_array($parameter['supervise_region_2']) || is_null($parameter['supervise_region_2']));
		$judge_5 = array_key_exists('supervise_region_3', $parameter) && (is_array($parameter['supervise_region_3']) || is_null($parameter['supervise_region_3']));
		$judge_6 = array_key_exists('idcard_frontend', $parameter) && is_string($parameter['idcard_frontend']) && (0 < strlen($parameter['idcard_frontend']));
		$judge_7 = array_key_exists('idcard_backend', $parameter) && is_string($parameter['idcard_backend']) && (0 < strlen($parameter['idcard_backend']));
		$judge_8 = array_key_exists('idcard_hold', $parameter) && is_string($parameter['idcard_hold']) && (0 < strlen($parameter['idcard_hold']));
		if(!$judge_1 || !$judge_2 || !$judge_3 || !$judge_4 || !$judge_5 || !$judge_6 || !$judge_7 || !$judge_8){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		// 获取当前用户的申请
		$StaffApplyModel = \app\common\model\StaffApplyModel::where('subscriber_id', $this->subscriber_id)
			->order('id DESC')->limit(1)->find();
		if(null !== $StaffApplyModel){
			if(0 === $StaffApplyModel->result){
				return $this->disposeJson(1, '您的申请正等待审批，请等待');
			}else if(1 === $StaffApplyModel->result){
				return $this->disposeJson(1, '您的申请已通过，请重新进入');
			};
		};
		
		// 负责地区壹
		$judge_1 = array_key_exists('province', $parameter['supervise_region_1']) && is_string($parameter['supervise_region_1']['province']) && (0 < strlen($parameter['supervise_region_1']['province']));
		$judge_2 = array_key_exists('city', $parameter['supervise_region_1']) && is_string($parameter['supervise_region_1']['city']) && (0 < strlen($parameter['supervise_region_1']['city']));
		$judge_3 = array_key_exists('district', $parameter['supervise_region_1']) && is_string($parameter['supervise_region_1']['district']) && (0 < strlen($parameter['supervise_region_1']['district']));
		if(!$judge_1 || !$judge_2 || !$judge_3){
			return $this->disposeJson(1, '位置壹参数校验失败');
		};
		if(in_array($parameter['supervise_region_1']['province'], array('北京市', '天津市', '重庆市', '上海市'))){
			$parameter['supervise_region_1']['address_full'] = $parameter['supervise_region_1']['province'] . $parameter['supervise_region_1']['district'];
		}else{
			$parameter['supervise_region_1']['address_full'] = $parameter['supervise_region_1']['province'] . $parameter['supervise_region_1']['city'] . $parameter['supervise_region_1']['district'];
		};
		$RegionModel = \app\common\model\RegionModel::where('name_full', $parameter['supervise_region_1']['address_full'])->find();
		if(null === $RegionModel){
			return $this->disposeJson(1, '负责地区1未找到');
		};
		$paramete['supervise_region_id'] = array();
		$paramete['supervise_region_id'][] = $RegionModel->id;
		$paramete['supervise_region_name'] = array();
		$paramete['supervise_region_name'][] = $RegionModel->name_full;
		
		// 负责地区贰
		if(is_array($parameter['supervise_region_2'])){
			$judge_1 = array_key_exists('province', $parameter['supervise_region_2']) && is_string($parameter['supervise_region_2']['province']) && (0 < strlen($parameter['supervise_region_2']['province']));
			$judge_2 = array_key_exists('city', $parameter['supervise_region_2']) && is_string($parameter['supervise_region_2']['city']) && (0 < strlen($parameter['supervise_region_2']['city']));
			$judge_3 = array_key_exists('district', $parameter['supervise_region_2']) && is_string($parameter['supervise_region_2']['district']) && (0 < strlen($parameter['supervise_region_2']['district']));
			if(!$judge_1 || !$judge_2 || !$judge_3){
				return $this->disposeJson(1, '位置壹参数校验失败');
			};
			if(in_array($parameter['supervise_region_2']['province'], array('北京市', '天津市', '重庆市', '上海市'))){
				$parameter['supervise_region_2']['address_full'] = $parameter['supervise_region_2']['province'] . $parameter['supervise_region_2']['district'];
			}else{
				$parameter['supervise_region_2']['address_full'] = $parameter['supervise_region_2']['province'] . $parameter['supervise_region_2']['city'] . $parameter['supervise_region_2']['district'];
			};
			$RegionModel = \app\common\model\RegionModel::where('name_full', $parameter['supervise_region_2']['address_full'])->find();
			if(null === $RegionModel){
				return $this->disposeJson(1, '负责地区1未找到');
			};
			if(!in_array($RegionModel->id, $paramete['supervise_region_id'])){
				$paramete['supervise_region_id'][] = $RegionModel->id;
				$paramete['supervise_region_name'][] = $RegionModel->name_full;
			};
		};
		
		// 负责地区叁
		if(is_array($parameter['supervise_region_3'])){
			$judge_1 = array_key_exists('province', $parameter['supervise_region_3']) && is_string($parameter['supervise_region_3']['province']) && (0 < strlen($parameter['supervise_region_3']['province']));
			$judge_2 = array_key_exists('city', $parameter['supervise_region_3']) && is_string($parameter['supervise_region_3']['city']) && (0 < strlen($parameter['supervise_region_3']['city']));
			$judge_3 = array_key_exists('district', $parameter['supervise_region_3']) && is_string($parameter['supervise_region_3']['district']) && (0 < strlen($parameter['supervise_region_3']['district']));
			if(!$judge_1 || !$judge_2 || !$judge_3){
				return $this->disposeJson(1, '位置壹参数校验失败');
			};
			if(in_array($parameter['supervise_region_3']['province'], array('北京市', '天津市', '重庆市', '上海市'))){
				$parameter['supervise_region_3']['address_full'] = $parameter['supervise_region_3']['province'] . $parameter['supervise_region_3']['district'];
			}else{
				$parameter['supervise_region_3']['address_full'] = $parameter['supervise_region_3']['province'] . $parameter['supervise_region_3']['city'] . $parameter['supervise_region_3']['district'];
			};
			$RegionModel = \app\common\model\RegionModel::where('name_full', $parameter['supervise_region_3']['address_full'])->find();
			if(null === $RegionModel){
				return $this->disposeJson(1, '负责地区1未找到');
			};
			if(!in_array($RegionModel->id, $paramete['supervise_region_id'])){
				$paramete['supervise_region_id'][] = $RegionModel->id;
				$paramete['supervise_region_name'][] = $RegionModel->name_full;
			};
		};
		
		// 保存图片 身份证正面照
		$target_path = ROOT_PATH . 'public' . DS . 'resource' . DS . 'image' . DS . 'idcard' . DS;
		$url_path = '/' . 'resource' . '/' . 'image' . '/' . 'idcard' . '/';
		$result = \Image::Base64SaveFile($parameter['idcard_frontend'], $target_path, $return_message);
		if(true !== $result){
			return $this->disposeJson(1, '身份证正面照' . $return_message);
		};
		$parameter['idcard_frontend'] = $url_path . $return_message;
		
		// 保存图片 身份证背面照
		$result = \Image::base64SaveFile($parameter['idcard_backend'], $target_path, $return_message);
		if(true !== $result){
			return $this->disposeJson(1, '身份证正面照' . $return_message);
		};
		$parameter['idcard_backend'] = $url_path . $return_message;
		
		// 保存图片 身份证持证照
		$result = \Image::base64SaveFile($parameter['idcard_hold'], $target_path, $return_message);
		if(true !== $result){
			return $this->disposeJson(1, '身份证正面照' . $return_message);
		};
		$parameter['idcard_hold'] = $url_path . $return_message;
		
		$time = time();
		\app\common\model\Model::startTrans();
		try{
			
			// 创建申请记录
			$StaffApplyModel = new \app\common\model\StaffApplyModel();
			$StaffApplyModel->subscriber_id = $this->subscriber_id;
			$StaffApplyModel->realname = $parameter['realname'];
			$StaffApplyModel->phone = $parameter['phone'];
			$StaffApplyModel->idcard_frontend = $parameter['idcard_frontend'];
			$StaffApplyModel->idcard_backend = $parameter['idcard_backend'];
			$StaffApplyModel->idcard_hold = $parameter['idcard_hold'];
			$StaffApplyModel->region_id = implode(',', $paramete['supervise_region_id']);
			$StaffApplyModel->region_name = implode(',', $paramete['supervise_region_name']);
			$StaffApplyModel->create_time = $time;
			$StaffApplyModel->result = 0;
			$result = $StaffApplyModel->save();
			if(false == $result){
				return $this->disposeJson(1, '申请提交失败，请稍后重试');
			};
			
			\app\common\model\Model::commit();
			
		}catch(\Exception $error){
			
			\app\common\model\Model::rollback();
			$message = is_string($error->getMessage()) ? $error->getMessage() : json_encode($error->getMessage());
			return $this->disposeJson(1, $error->getMessage());
			
		};
		
		// 返回结果
		return $this->disposeJson(0, '提交成功');
		
	}
	
	public function get_setting() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('field', $parameter) && is_array($parameter['field']) && (0 < count($parameter['field']));
		if(!$judge_1){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$id_list = array();
		foreach($parameter['field'] as $field_name){
			if(!is_string($field_name) || (0 === strlen($field_name))){
				return $this->disposeJson(1, '查询的项不正确');
			}else{
				$id = \app\common\model\SettingModel::getFieldsId($field_name);
				if(null === $id){
					return $this->disposeJson(1, '获取数据ID失败');
				}else{
					$id_list[] = $id;
				};
			};
		};
		
		$field = array('id', 'value');
		$setting_select = \app\common\model\SettingModel::field($field)->where('id', 'in', $id_list)->select();
		
		$setting_list = array();
		foreach($setting_select as $SettingModel){
			$name = $SettingModel->getFieldsName();
			$setting_list[$name] = $SettingModel->getData('value');
		};
		
		return $this->disposeJson(0, '', $setting_list);
	}
	
}